1520D - Same Differences - CodeForces Solution


data structures hashing math *1200

Please click on ads to support us..

Python Code:

from collections import *

tc = int(input())
for _ in range(tc):
    input()
    l = [x - i for i, x in enumerate(map(int, input().split()))]
    g = Counter(l)
    ans = 0
    for k in g:
        ans += (g[k] * (g[k]-1)) // 2
    print(ans)

   			  			    	   			 	 	 	

C++ Code:

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <numeric>
#include <vector>
#include <set>
#include <map>
#include <queue>

using namespace std;
#define ll long long

ll change(int n){
    return 1LL * n*(n-1)/2;
}
 
void solve(){
    int n;
    cin >> n;
    map<int,int> freq;
    for(int i = 0; i < n; i++){
        int x; cin >> x;
        x -= i;
        freq[x]++;
    }
    ll ans = 0;
    for(auto it = freq.begin(); it != freq.end(); it++){
        ans += change((*it).second);
    }
    cout << ans << "\n";
}
            
int main(){
    ios::sync_with_stdio(0);cin.tie(0);
    int t = 1; cin >> t;
    while(t--) solve();
}


Comments

Submit
0 Comments
More Questions

975C - Valhalla Siege
518B - Tanya and Postcard
514B - Han Solo and Lazer Gun
898B - Proper Nutrition
9C - Hexadecimal's Numbers
1265B - Beautiful Numbers
745A - Hongcow Learns the Cyclic Shift
873A - Chores
1754B - Kevin and Permutation
1547D - Co-growing Sequence
1754D - Factorial Divisibility
1117B - Emotes
412B - Network Configuration
845B - Luba And The Ticket
1732A - Bestie
389A - Fox and Number Game
1732B - Ugu
1100B - Build a Contest
1181B - Split a Number
1313B - Different Rules
1736D - Equal Binary Subsequences
1754A - Technical Support
26B - Regular Bracket Sequence
699A - Launch of Collider
474D - Flowers
1016A - Death Note
1335C - Two Teams Composing
1167C - News Distribution
813C - The Tag Game
1130C - Connect